package com.runa.monitor.platinterface.extend.nb.mqtt;

import com.ctiot.aep.mqmsgpush.sdk.IMsgConsumer;
import com.ctiot.aep.mqmsgpush.sdk.IMsgListener;
import com.ctiot.aep.mqmsgpush.sdk.MqMsgConsumer;
import com.runa.monitor.platinterface.extend.nb.config.MqttNbIotConfig;
import com.runa.monitor.platinterface.extend.nb.config.NewMqttNbIotConfig;
import com.runa.monitor.platinterface.extend.nb.utils.MqttSetUtils;
import com.runa.monitor.platinterface.extend.nb.utils.NewMqttSetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class MqttInitServer implements DisposableBean {
    @Resource
    private MqttNbIotConfig mqttNbIotConfig;

    @Resource
    private NewMqttNbIotConfig newMqttNbIotConfig;
    /**
     * 消息服务地址
     */
    private String serverUrl;
    /**
     * 直接填空字符串，CA证书，JDK已经内置相关根证书，无需指定
     */
    private final String certFilePath = "";
    /**
     * 租户id
     */
    private String tenantId;
    /**
     * 身份认证token
     */
    private String token;
    /**
     * 数据上传主题
     */
    private String dataTopic;
    /**
     * 新租户-消息服务地址
     */
    private String newServerUrl;
    /**
     * 新租户-直接填空字符串，CA证书，JDK已经内置相关根证书，无需指定
     */
    private final String newCertFilePath = "";
    /**
     * 新租户-租户id
     */
    private String newTenantId;
    /**
     * 新租户-身份认证token
     */
    private String newToken;
    /**
     * 新租户-数据上传主题
     */
    private String newDataTopic;

    /**
     * 创建消息接收类
     */
    static IMsgConsumer consumer = new MqMsgConsumer();

    /**
     * 新租户-创建消息接收类
     */
    static IMsgConsumer newConsumer = new MqMsgConsumer();

    @Resource
    private IMsgListener msgListener;

    @PostConstruct
    public void init() {
        initParamConfig();
        if (null == mqttNbIotConfig.getEnable() || !mqttNbIotConfig.getEnable()) {
            log.info("------nb-iot设备---mqtt监听接收消息功能未启用------");
        }else{
            try {
                //初始化
                List<String> topics = Arrays.asList(dataTopic.split(","));
                /**
                 * @param server  消息服务server地址
                 * @param tenantId 租户Id
                 * @param token    用户认证token
                 * @param certFilePath 证书文件路径
                 * @param topicNames   主题名列表，如果该列表为空或null，则自动消费该租户所有主题消息
                 * @param msgListener 消息接收者
                 * @return 是否初始化成功
                 */
                consumer.init(serverUrl, tenantId, token, certFilePath, topics, msgListener);
                log.info("------nbLot设备---mqtt开始监听接收消息------");
                //开始接收消息
                consumer.start();
            } catch (Exception e) {
                
            }
        }








        initNewParamConfig();
        if (null == newMqttNbIotConfig.getEnable() || !newMqttNbIotConfig.getEnable()) {
            log.info("------new-nb-iot设备---mqtt监听接收消息功能未启用------");
        }else{
            try {
                //初始化
                List<String> topics = Arrays.asList(newDataTopic.split(","));
                /**
                 * @param server  消息服务server地址
                 * @param tenantId 租户Id
                 * @param token    用户认证token
                 * @param certFilePath 证书文件路径
                 * @param topicNames   主题名列表，如果该列表为空或null，则自动消费该租户所有主题消息
                 * @param msgListener 消息接收者
                 * @return 是否初始化成功
                 */
                newConsumer.init(newServerUrl, newTenantId, newToken, newCertFilePath, topics, msgListener);
                log.info("------new-nbLot设备---mqtt开始监听接收消息------");
                //开始接收消息
                newConsumer.start();
            } catch (Exception e) {
                
            }
        }


    }


    /**
     * 设置基础参数
     */
    private void initParamConfig() {
        this.serverUrl = mqttNbIotConfig.getServerUrl();
        this.tenantId = mqttNbIotConfig.getTenantId();
        this.token = mqttNbIotConfig.getToken();
        this.dataTopic = mqttNbIotConfig.getDataTopic();
        MqttSetUtils.APP_KEY = mqttNbIotConfig.getAppKey();
        MqttSetUtils.APP_SECRET = mqttNbIotConfig.getAppSecret();
        MqttSetUtils.PANEL_PRODUCT_ID = mqttNbIotConfig.getPanelProductId();
        MqttSetUtils.PANEL_MASTER_KEY = mqttNbIotConfig.getPanelMasterKey();
        MqttSetUtils.RU_PANEL_PRODUCT_ID = mqttNbIotConfig.getRuPanelProductId();
        MqttSetUtils.RU_PANEL_MASTER_KEY = mqttNbIotConfig.getRuPanelMasterKey();
        MqttSetUtils.BUILD_METER_PRODUCT_ID = mqttNbIotConfig.getBuildMeterProductId();
        MqttSetUtils.BUILD_METER_MASTER_KEY = mqttNbIotConfig.getBuildMeterMasterKey();
        MqttSetUtils.BUILD_VALVE_PRODUCT_ID = mqttNbIotConfig.getBuildValveProductId();
        MqttSetUtils.BUILD_VALVE_MASTER_KEY = mqttNbIotConfig.getBuildValveMasterKey();
        MqttSetUtils.TEMPERATURE_PRESSURE_PRODUCT_ID = mqttNbIotConfig.getTemperaturePressureProductId();
        MqttSetUtils.TEMPERATURE_PRESSURE_MASTER_KEY = mqttNbIotConfig.getTemperaturePressureMasterKey();
    }

    /**
     * 设置基础参数
     */
    private void initNewParamConfig() {
        this.newServerUrl = newMqttNbIotConfig.getServerUrl();
        this.newTenantId = newMqttNbIotConfig.getTenantId();
        this.newToken = newMqttNbIotConfig.getToken();
        this.newDataTopic = newMqttNbIotConfig.getDataTopic();
        NewMqttSetUtils.APP_KEY = newMqttNbIotConfig.getAppKey();
        NewMqttSetUtils.APP_SECRET = newMqttNbIotConfig.getAppSecret();
        NewMqttSetUtils.RUI_PANEL_PRODUCT_ID = newMqttNbIotConfig.getRuiPanelProductId();
        NewMqttSetUtils.RUI_PANEL_MASTER_KEY = newMqttNbIotConfig.getRuiPanelMasterKey();
    }

    @Override
    public void destroy() {
        //程序退出时，停止接收、销毁
        MqttInitServer.consumer.stop();
        MqttInitServer.consumer.destroy();
        log.info("------电信mqtt退出监听------");
        //程序退出时，停止接收、销毁
        MqttInitServer.newConsumer.stop();
        MqttInitServer.newConsumer.destroy();
        log.info("------电信mqtt退出监听------");
    }
}
